<?php
/**
 * Created by PhpStorm.
 * User: suncco
 * Date: 2018/8/7
 * Time: 15:26
 */

namespace app\admin\controller;

use app\admin\logic\LinksLogic;
class Links extends Base
{
    public function index(){
        $link = db('links');
        $pagesize = 10;#每页数量
        $keyword = input('keyword');
        $order = input('order');
        $keyword = isset($keyword) ? htmlentities($keyword) : '';
        $order = isset($order) ? $order : 'DESC';
        $where = '';
        if ($keyword) {
            $where .= "title like '%{$keyword}%' ";
        }
        //默认按照时间降序
        $orderby = "t desc";
        if ($order == "asc") {

            $orderby = "t asc";
        }
        $list = $link
            ->where($where)
            ->order($orderby)
            ->paginate($pagesize,false,['query'=>request()->param()]);
//        dump( Db::getLastSql());die;

        $this->assign('list', $list);
        return view();
    }

    public function add(){
        return view('form');
    }

    public function edit(){
        $links_id = input('links_id');
        $links = db('links');

        $links = $links
                ->where('links_id',$links_id)
                ->find();

        $this->assign('links',$links);
        return view('form');
    }

    public function update(){
        $info = input('post.');
        $links_id = intval($info['links_id']);
        $data['title'] = isset($info['title']) ? $info['title'] : false;
        $data['url'] = isset($info['url']) ? $info['url'] : false;
        $data['o'] = isset($info['o']) ? $info['o'] : 0;
        $data['logo'] = $info['logo'];
        $data['t'] = time();

        if ($links_id) {
            $act = 'add';
        }else{
            $act = 'edit';
            $data['links_id'] = $links_id;
        }

        $validate = $this->validate($data, 'Links.'.$act, [], true);

        if ($validate !== true) {
            return json(['flag'=>1,'message'=>'警告！友链名称、友链链接为必填项目。']);
        }

        if ($links_id) {
            db('links')->data($data)->where('links_id=' . $links_id)->update();
            addlog('编辑友链，Links_ID：' . $links_id);
            return json(['flag'=>0,'message'=>'恭喜！友链编辑成功！']);
        } else {
            $links_id = db('links')->data($data)->insert();
            if ($links_id) {
                addlog('新增友链，Links_ID：' . $links_id);
                return json(['flag'=>0,'message'=>'恭喜！友链新增成功！']);
            } else {
                return json(['flag'=>1,'message'=>'抱歉，未知错误！']);
            }

        }
    }

    public function del(){
        $links_id = input('links_id');
        $links_id = isset($links_id) ? $links_id : false;

        if ($links_id) {
            if (strpos($links_id, ',')) {

//                $links_id = implode(',', $links_id);
                $map = [['links_id','in',$links_id],];
            } else {

                $map = 'links_id=' . $links_id;
            }
            if (db('links')->where($map)->delete()) {
                addlog('删除友链，LinkID：' . $links_id);
                return json(['flag'=>0,'message'=>'恭喜，友链删除成功！']);
            } else {
                return json(['flag'=>1,'message'=>'删除失败！']);
            }
        } else {
            return json(['flag'=>1,'message'=>'参数错误！']);
        }
    }
}